package com.example.flower_shop.controller.adm;

import com.example.flower_shop.common.PageResult;
import com.example.flower_shop.service.adm.AdmArticleService;
import com.example.flower_shop.vo.article.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

@Tag(name = "后台文章管理", description = "后台文章管理")
@RestController
@RequestMapping("/api/admin/article")
public class AdmArticleController {

    @Resource
    private AdmArticleService articleService;

    @GetMapping
    @Operation(summary = "查询所有文章", description = "查询所有文章")
    public PageResult<ArticlePageResp> findAllArticle(@Valid ArticlePageReq req) {
        return articleService.findAllArticle(req);
    }

    @GetMapping("/{articleId}")
    @Operation(summary = "获取文章详情", method = "GET",
            parameters = {
                    @Parameter(name = "articleId", description = "文章ID", required = true, in = ParameterIn.PATH)
            })
    public ArticleRespVO getArticle(@PathVariable Long articleId) {
        return articleService.getArticle(articleId);
    }

    @PutMapping("/status")
    @Operation(summary = "修改文章状态", description = "修改文章状态")
    public void update(@Valid AdmArticleUpdateReq req) {
        articleService.updateArticle(req);
    }

    @PostMapping("")
    @Operation(summary = "创建文章", method = "POST",
            requestBody = @RequestBody(
                    content = @Content(
                            mediaType = MediaType.MULTIPART_FORM_DATA_VALUE,
                            schema = @Schema(
                                    implementation = ArticleReq.class,
                                    name = "ArticleReq",
                                    description = "创建文章请求参数"
                            )

                    )
            ))
    public void createArticle(@Valid ArticleReq req) {
        articleService.createArticle(req);
    }

    @PutMapping("")
    @Operation(summary = "更新文章", method = "PUT",
            requestBody = @RequestBody(
                    content = @Content(
                            mediaType = MediaType.MULTIPART_FORM_DATA_VALUE,
                            schema = @Schema(
                                    implementation = ArticleReq.class,
                                    name = "ArticleReq",
                                    description = "更新文章请求参数"
                            )

                    )
            ))
    public void updateArticle(@Valid ArticleReq req) {
        articleService.updateArticleInfo(req);
    }

    @DeleteMapping
    @Operation(summary = "删除文章", description = "删除文章",
            parameters = {
                    @Parameter(name = "ids", description = "文章id数组", required = true)
            })
    public void delete(@RequestParam Long[] ids) {
        articleService.batchDelete(ids);
    }
}
